﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MapEditorSDG
{

    public partial class frmEditor : Form
    {
        public const string DEFAULT_FIELD_TEXT = "[Type de terrain]";

        //Default values given to the panel to locate it and size it.
        public const int PANEL_LOCATION_X = 15;
        public const int PANEL_LOCATION_Y = 90;

        //Both, width and height, are calculated by their size in cases.
        public const int PANEL_DEFAULT_CELL_WIDTH = 1;
        public const int PANEL_DEFAULT_CELL_HEIGHT = 1;

        private int columns;
        private int rows;
        private GridHandler panMap;

        public frmEditor()
        {
            columns = 0;
            rows = 0;

            InitializeComponent();
            panMap = new GridHandler(PANEL_LOCATION_X, PANEL_LOCATION_Y, 0, 0, this);
        }

        private void cmdGridModify_Click(object sender, EventArgs e)
        {
            panMap.ImgSrc = "";
            int tmpColumns = 0;
            int tmpRows = 0;

            bool isWNumeric = Int32.TryParse(txtWidth.Text, out tmpColumns);
            bool isHNumeric = Int32.TryParse(txtHeight.Text, out tmpRows);

            if (isWNumeric && isHNumeric)
            {
                //Check if the creation of a new array is needed.
                if (columns != tmpColumns || rows != tmpRows)
                {
                    columns = tmpColumns;
                    rows = tmpRows;

                    GameCell[,] gameCells = new GameCell[columns, rows];

                    //Create cells to fill the panel.
                    for (int i = 0; i < rows; i++)
                        for (int j = 0; j < columns; j++)
                            gameCells[j, i] = new GameCell(panMap, i, j);
                       
                    panMap.resize(rows, columns, gameCells);
                }
            }

           ResizeForm();
           ResetFields();
           panMap.ApplyImage();
        }

        private void cmdApply_Click(object sender, EventArgs e)
        {
            //Apply change on selected cells.
            for (int i = 0; i < panMap.GameCells.GetLength(1); i++)
            {
                for (int j = 0; j < panMap.GameCells.GetLength(0); j++)
                {
                    if (panMap.GameCells[j, i].IsSelected)
                    {
                        panMap.GameCells[j, i].IsSelected = false;
                        panMap.GameCells[j, i].ApplyChange((string)cmbField.SelectedItem, chkSpawn.Checked, (cmbLevel.SelectedIndex - 3));
                    }
                }
            }

            ResizeForm();
            ResetFields();
        }

        private void cmdCellReset_Click(object sender, EventArgs e)
        {
            //Apply change on selected cells.
            for (int i = 0; i < panMap.GameCells.GetLength(1); i++)
                for (int j = 0; j < panMap.GameCells.GetLength(0); j++)
                    if (panMap.GameCells[j, i].IsSelected)
                        panMap.GameCells[j, i].Reset(j, i);

            ResetFields();
        }

        private void toolStrpNew_Click(object sender, EventArgs e)
        {
            FileHandler.NewMap(panMap);
        }

        private void toolStrpLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDiag = new OpenFileDialog();
            fileDiag.Title = "Select a file";
            fileDiag.ShowDialog();
            FileHandler.LoadMapFromJSON(fileDiag.FileName, panMap);
            ResizeForm();

            //panMap.resize(panMap.GameCells.GetLength(1), panMap.GameCells.GetLength(0), panMap.GameCells);
        }

        private void toolStrpSave_Click(object sender, EventArgs e)
        {
            //FileHandler.SaveMap("test.txt", panMap);
            SaveFileDialog fileDiag = new SaveFileDialog();
            fileDiag.Title = "Save as";
            fileDiag.ShowDialog();
            FileHandler.SaveMapToJSON(fileDiag.FileName + ".sdg", panMap);
        }

        private void frmEditor_Load(object sender, EventArgs e)
        {
            //This order should not be change to follow the order of the enum AllFieldTypes in the GameCell class.
            cmbField.Items.Add(GameCell.STRING_TYPE_VOID);
            cmbField.Items.Add(GameCell.STRING_TYPE_PLAIN);
            cmbField.Items.Add(GameCell.STRING_TYPE_THUNDRA);
            cmbField.Items.Add(GameCell.STRING_TYPE_DESERT);
            cmbField.Items.Add(GameCell.STRING_TYPE_HILL);
            cmbField.Items.Add(GameCell.STRING_TYPE_MOUNTAIN);
            cmbField.Items.Add(GameCell.STRING_TYPE_RIVER);
            cmbField.Items.Add(GameCell.STRING_TYPE_WATER);
            cmbField.Items.Add(GameCell.STRING_TYPE_SWAMP);
            cmbField.Items.Add(GameCell.STRING_TYPE_BUSH);
            cmbField.Items.Add(GameCell.STRING_TYPE_ICE);
            cmbField.Items.Add(GameCell.STRING_TYPE_LAVA);
            cmbField.Items.Add(GameCell.STRING_TYPE_SNOW);
            cmbField.Items.Add(GameCell.STRING_TYPE_ROAD);
            cmbField.Items.Add(GameCell.STRING_TYPE_NOMANLAND);
            cmbField.Items.Add(GameCell.STRING_TYPE_VULCAN);
            cmbField.Items.Add(GameCell.STRING_TYPE_PRECARIOUS_MOUNTAIN);
            cmbField.Items.Add(GameCell.STRING_TYPE_DEEP_WATER);

            //Events list.
            cmbEvent.Items.Add(GridHandler.EVENTNAME_NONE);
            cmbEvent.Items.Add(GridHandler.EVENTNAME_TSUNAMI);

            //Put the level of a cell to '0' by default.
            cmbLevel.SelectedIndex = 3;
        }

        private void ResetFields()
        {
            cmbField.SelectedIndex = 1;
            chkSpawn.Checked = false;
            cmbLevel.SelectedIndex = 3;
            cmbEvent.SelectedIndex = 0;
        }

        private void ResizeForm()
        {
            int newFormHeight = frmEditor.ActiveForm.Size.Height;
            int newFormWidth = frmEditor.ActiveForm.Size.Width;
            bool resizeNeeded = false;

            //Look if the form is large enough to hold the panel.
            if (panMap.Size.Width + PANEL_LOCATION_X * 2 > newFormWidth)
            {
                newFormWidth = panMap.Size.Width + PANEL_LOCATION_X * 3;
                resizeNeeded = true;
            }

            //Look if the form is tall enough to hold the panel.
            if (panMap.Size.Height + PANEL_LOCATION_Y * 2 > newFormHeight)
            {
                newFormHeight = panMap.Size.Height + PANEL_LOCATION_Y * 2;
                resizeNeeded = true;
            }

            if (resizeNeeded)
                this.SetBounds(this.Location.X, this.Location.Y, newFormWidth, newFormHeight);

            this.Refresh();
            this.Invalidate();
        }

        private void toolStrpQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog imgDiag = new OpenFileDialog();
            imgDiag.Title = "Choose your destiny";
            imgDiag.ShowDialog();
            panMap.ImgSrc = imgDiag.FileName;

            //Add an image to the panel.
            panMap.ApplyImage();
        }
    }
}
